<template>
	<view>
		<el-popup ref="popup" direction="bottom" backgroundColor="rgb(248, 248, 248)" @close="onClose">
			<view class="title">请选择开票信息</view>
			<view style="width: 100%; height: 70rpx;"></view>
			<scroll-view :style="{ 'height' : (windowHeight / 2) + 'px' }" scroll-y>
				<view class="wrapper">
					<view class="list-wrap">
						<view 
						class="list-box" 
						v-for="(item,index) in list" 
						:key="index"
						@click="onSelected(item)"
						>
							<view class="address">
								<view>
									<text>{{ item.type == 1 ? "个人发票" : "企业发票" }}</text>
									<text>{{ item.name }}</text>
								</view>
								<view>
									<view v-if="item.type == 1">
										<view>个人邮箱：{{ item.email }}</view>
										<view>电话号码：{{ item.mobile }}</view>
									</view>
									<view v-else>
										<view>企业邮箱：{{ item.email }}</view>
										<view>企业税号：{{ item.duty_number }}</view>
									</view>
								</view>
							</view>
							<view class="selected">
								<view :class="{ active: !isEmpty(invoice) && item.id == invoice.id }">
									<uni-icons class="icon" type="checkmarkempty" color="#fff" size="12"></uni-icons>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			
			<view class="footer">
				<text class="btn" @click="navigateTo('/pages/ucenter/invoice/editor')">新增发票</text>
				<text class="cancel" @click="onCancel">不开发票</text>
			</view>
		</el-popup>
	</view>
</template>

<script setup>
	import { nextTick, onMounted, ref, watch } from "vue";
	import { inArray,isEmpty } from "@/el-ui/libs/check.js";
	import { navigateTo } from "@/el-ui/libs/navigate";
	
	const props = defineProps({
		modelValue: {
			type: Boolean,
			default: false
		}
	});
	
	const emits = defineEmits(["modelValue","update:modelValue","change"]);
	
	const { windowHeight } = uni.getSystemInfoSync();
	
	const popup = ref();
	onMounted(()=>{
		nextTick(()=>{
			props.modelValue && popup.value?.open();
			getList();
		})
	})
	
	const invoice = ref({});
	const list 	  = ref([]);
	const getList = ()=>{
		uni.$http.get("checkout/invoice").then(res=>{
			if(res.code){
				list.value = res.data;
			}
		});
	}
	
	const onSelected = (value)=>{
		invoice.value = value;
		emits("change",value);
		emits("update:modelValue",false);
	}
	
	const onCancel = ()=>{
		invoice.value = {};
		emits("change",{});
		emits("update:modelValue",false);
	}
	
	const onClose = ()=>{
		emits("update:modelValue",false);
	}
	
	watch(()=>props.modelValue,(value)=>{
		if(value){
			nextTick(()=>{
				popup.value?.open();
			})
		}else{
			nextTick(()=>{
				popup.value?.close();
			})
		}
	});
	
</script>

<style lang="scss" scoped>
	.title {
		width: 100%;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		font-size: 32rpx;
		position: absolute;
		left: 0;
		top: 0px;
		background-color: #fff;
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
	}
	.wrapper {
		padding: 0rpx;
		margin-top: 20rpx;
		.list-wrap {
			display: flex;
			flex-direction: column;
			.list-box {
				background-color: #fff;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx 30rpx;
				border-radius: 15rpx;
				font-size: 28rpx;
				margin-bottom: 20rpx;
				.address {
					view {
						&:nth-child(1){
							text {
								&:nth-child(1){
									
								}
								&:nth-child(2){
									padding-left: 10rpx;
								}
							}
						}
						&:nth-child(2){
							padding-top: 10rpx;
						}
					}
				}
				.selected {
					view {
						width: 15px;
						height: 15px;
						background-color: #bbb;
						padding: 5rpx;
						border-radius: 50%;
						position: relative;
						&.active {
							background-color: #c21313;
						}
						.icon {
							position: absolute;
							left: 9rpx;
							top: 10rpx;
						}
					}
				}
			}
		}
	}
	.footer {
		width: 100%;
		height: 220rpx;
		background-color: #fff;
		position: absolute;
		left: 0;
		bottom: 0;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		.btn {
			display: block;
			width: 95%;
			background: linear-gradient(90deg, var(--ui-bg-color-light), var(--ui-bg-color));
			font-size: 28rpx;
			height: 70rpx;
			line-height: 70rpx;
			text-align: center;
			color: #fff;
			border-radius: 50rpx;
			margin-bottom: 20rpx;
		}
		.cancel {
			display: block;
			width: 95%;
			background: #fff;
			font-size: 28rpx;
			height: 70rpx;
			line-height: 70rpx;
			text-align: center;
			color: #333;
			border-radius: 50rpx;
			border: 1px solid #ddd;
		}
	}
</style>